﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;


namespace FXnaGraphics
{
    public class ServiceContainer : IServiceProvider
    {

        Dictionary<Type, object> services = new Dictionary<Type, object>();


        /// <summary>
        /// Adds a new service to the collection.
        /// </summary>
        public void AddService<T>(T service)
        {
            this.AddService(typeof(T), service);
        }


        public void AddService(Type type, object provider)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type", "Resources.ServiceTypeCannotBeNull");
            }
            if (provider == null)
            {
                throw new ArgumentNullException("provider", "Resources.ServiceProviderCannotBeNull");
            }
            if (this.services.ContainsKey(type))
            {
                throw new ArgumentException("Resources.ServiceAlreadyPresent", "type");
            }


            this.services.Add(type, provider);

        }





        /// <summary>
        /// Looks up the specified service.
        /// </summary>
        public object GetService(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type", "Resources.ServiceTypeCannotBeNull");
            }
            if (this.services.ContainsKey(type))
            {
                return this.services[type];
            }
            return null;

        }

        public void RemoveService(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type", "service can't be null");
            }
            this.services.Remove(type);
        }


    }
}
